package com.liyuxiang.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liyuxiang.yygh.common.exception.YyghException;
import com.liyuxiang.yygh.common.result.Result;
import com.liyuxiang.yygh.common.utils.MD5;
import com.liyuxiang.yygh.hosp.service.HospitalSetService;
import com.liyuxiang.yygh.model.hosp.HospitalSet;
import com.liyuxiang.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@Api(tags = "医院设置管理")
public class HospitalSetController {
    @Autowired
    HospitalSetService hospitalSetService;

    //查询所有医院设置
    @GetMapping("/findAll")
    @ApiOperation(value = "获取所有医院设置信息")
    public Result findAllHospitalSet() {
        try {
            List<HospitalSet> list = hospitalSetService.list();
            return Result.ok(list);
        } catch (Exception e) {
            throw new YyghException("失败", 201);
        }

    }

    //根据id逻辑删除医院设置信息
    @DeleteMapping("{id}")
    @ApiOperation(value = "根据id逻辑删除医院设置信息")
    public Result removeHospSet(@PathVariable("id") Long id) {
        boolean flag = hospitalSetService.removeById(id);
        return flag ? Result.ok() : Result.fail();
    }

    //分页查询医院设置
    @PostMapping("/findPage/{current}/{limit}")
    public Result findPageHospitalSet(@PathVariable("current") Long current, @PathVariable("limit") Long limit, @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        Page<HospitalSet> page = new Page<>(current, limit);
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        //y医院名称
        String hosname = hospitalSetQueryVo.getHosname();
        //医院编号
        String hoscode = hospitalSetQueryVo.getHoscode();
        if (!StringUtils.isEmpty(hosname)) {
            wrapper.like("hosname", hosname);
        }
        if (!StringUtils.isEmpty(hoscode)) {
            wrapper.eq("hoscode", hoscode);
        }
        Page<HospitalSet> result = hospitalSetService.page(page, wrapper);
        return Result.ok(result);
    }

    //新增医院设置
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        //设置状态  1使用，0不能使用
        hospitalSet.setStatus(1);
        //签名密钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        boolean flag = hospitalSetService.save(hospitalSet);
        return flag ? Result.ok() : Result.fail();
    }

    //根据id获取医院设置信息
    @GetMapping("getHospitalSet/{id}")
    public Result getHospitalSet(@PathVariable("id") Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    //修改医院生物质
    @PostMapping("/updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        boolean flag = hospitalSetService.updateById(hospitalSet);
        return flag ? Result.ok() : Result.fail();
    }

    //批量删除医院设置
    @DeleteMapping("/batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList) {
        boolean flag = hospitalSetService.removeByIds(idList);
        return flag ? Result.ok() : Result.fail();
    }

    //医院设置锁定和解锁
    @PutMapping("/lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        LambdaUpdateWrapper<HospitalSet> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(HospitalSet::getId, id);
        updateWrapper.set(HospitalSet::getStatus, status);
        boolean flag = hospitalSetService.update(updateWrapper);
        return flag ? Result.ok() : Result.fail();
    }

    //发送签名密钥
    @PutMapping("sendKey/{id}")
    public Result lockHospitalSet(@PathVariable("id") Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO 发送短信
        return Result.ok();
    }


}
